<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // var c = console.log.bind(document)
        // var obj = {
        //     username:'快乐的小青蛙',
        //     psw:'123456'
        // }
        // var arr =[1,2,3]
        
        // // 可以存储基本数据类型
        // localStorage.name = '李四'  // name	李四           
        // localStorage.age = 20     // age	20
        // localStorage.trues = true     // age	20
        // localStorage.truess = undefined     // age	20

        // // 存储复杂数据类型只会出现存储的类型
        // localStorage.obj = obj      // obj	[object Object]
        // localStorage.arr =  arr     // arr	1,2,3 转换成字符串
        // localStorage.username = obj.username // username	快乐的小青蛙

        // c(localStorage.name)    // 李四
        // c(localStorage.age)     // 20
        // c(localStorage.getItem('name')) // 李四
        // // c(JSON.parse('name'))   // 报错

        // localStorage.obj = JSON.stringify(obj)
        // c(localStorage.obj)  // {"username":"快乐的小青蛙","psw":"123456"}
        // c(JSON.parse(localStorage.obj)) // {username: '快乐的小青蛙', psw: '123456'}
        // function ad(){
        //     return JSON.parse(localStorage.obj) 
        // }
        // c(ad(obj)) // {username: '快乐的小青蛙', psw: '123456'}
        // localStorage.obj = JSON.parse(localStorage.obj) 
        // c(localStorage.obj) // [object Object]

    var obj = {
        username: '快乐的小青蛙',
        psw: '123456'
    }
    var arr = [1, 2, 3]

    /* 
        取值  需要传入 key
        存值  需要传入 key 及 value
        key:属性值		value:属性值
     */

//    判断是存储还是获取
function storage(key,value){
    if(value){
        if(typeof value == 'object'){
            value = JSON.stringify(value)
        }
        localStorage[key]=value
    }else {
        if(localStorage[key]){
            return localStorage[key]
        }else{
            return ''
        }
    }
}
var c = console.log.bind(document)
storage('user',arr)
storage('user1',obj)
storage('user2','李四')
storage('user3','20')
c(storage('user'))
c(storage('user1'))
c(storage('user2'))
c(storage('user3'))

</script>
    
</body>
</html>